package XunChunCPU.EXE

import chisel3._ 
import chisel3.util._

import XunChunCPU.common.CommonConfig._ 
import XunChunCPU.common.Bundles._

class EXEReg extends Module{
    val io = IO(new Bundle{
        val exeInfo_in = Flipped(new ExeInfo)
        val exeInfo_out = new ExeInfo
        val valid_in = Input(Bool())
        val ready_in = Input(Bool())
        val ready_out = Output(Bool())
    })
    io.ready_out := io.ready_in
    val OperAtmp = RegInit(0.U(regLen.W))
    val OperBtmp = RegInit(0.U(regLen.W))
    val regwetmp = RegInit(false.B)
    val wAddrtmp = RegInit(0.U(regAddrLen.W))
    val optmp = RegInit(0.U(8.W))
    val AFromRegtmp = RegInit(false.B)
    val BFromRegtmp = RegInit(false.B)
    val ARegAddr = RegInit(0.U(5.W))
    val BRegAddr = RegInit(0.U(5.W))
    // 数据合法并且执行模块准备完毕
    when(io.valid_in && io.ready_in){
        OperAtmp := io.exeInfo_in.OperA
        OperBtmp := io.exeInfo_in.OperB
        regwetmp := io.exeInfo_in.regwe
        wAddrtmp := io.exeInfo_in.wAddr
        optmp := io.exeInfo_in.op
        AFromRegtmp := io.exeInfo_in.AFromReg
        BFromRegtmp := io.exeInfo_in.BFromReg
        ARegAddr := io.exeInfo_in.ARegAddr
        BRegAddr := io.exeInfo_in.BRegAddr
    }.elsewhen(io.ready_in===false.B){
        // 没有准备好
        OperAtmp := io.exeInfo_out.OperA
        OperBtmp := io.exeInfo_out.OperB
        regwetmp := io.exeInfo_out.regwe
        wAddrtmp := io.exeInfo_out.wAddr
        optmp := io.exeInfo_out.op
        AFromRegtmp := io.exeInfo_out.AFromReg
        BFromRegtmp := io.exeInfo_out.BFromReg
        ARegAddr := io.exeInfo_out.ARegAddr
        BRegAddr := io.exeInfo_out.BRegAddr
    }otherwise{
        OperAtmp := 0.U
        OperBtmp := 0.U
        regwetmp := false.B
        wAddrtmp := 0.U
        optmp := 0.U
        AFromRegtmp := false.B
        BFromRegtmp := false.B
        ARegAddr := 0.U
        BRegAddr := 0.U
    }
    io.exeInfo_out.OperA := OperAtmp
    io.exeInfo_out.OperB := OperBtmp
    io.exeInfo_out.op := optmp
    io.exeInfo_out.regwe := regwetmp
    io.exeInfo_out.wAddr := wAddrtmp
    io.exeInfo_out.AFromReg := AFromRegtmp
    io.exeInfo_out.BFromReg := BFromRegtmp
    io.exeInfo_out.ARegAddr := ARegAddr
    io.exeInfo_out.BRegAddr := BRegAddr
}